/*
 * @(#)DateUtility.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework;

import java.util.Calendar;

/**
 * DateUtility.java
 *
 * Common date utility functions which used in systems.
 *
 *   @author            Lun Au
 *   @company       DCIVision Limited
 *   @creation date  26/09/2006
 *   @version          $Revision: 1.2 $
 */

public class DateUtility {

    public static long abs_diff ( int field, Calendar calStart, Calendar calEnd ) {
        return Math.abs( diff ( field, calStart, calEnd ) );
    }

    public static long diff ( int field, Calendar calStart, Calendar calEnd ) {
        if(calStart.after(calEnd)){
            Calendar calTemp = calStart;
            calStart = calEnd;
            calEnd = calTemp;
        }
       long diff = 0;

      while ( calStart.before(calEnd)) {
           calStart.add(field, 1);
           if(calStart.after(calEnd)){
               calStart.add(field, -1);
               break;
           }
           diff++;
       }

        diff = diff * (calStart.before(calEnd)?1:-1);
        return diff;
    }

}